import dov from '../../utils/dov.min'
const regeneratorRuntime = require('../../utils/runtime')
var WxParse = require('../../wxParse/wxParse')
// pages/main/main.js
Page({
  /**
   * Page initial data
   */
  data: {
      // 获取商品时的请求参数
      query: {
        query: '',
        pagenum: 1,
        pagesize: 1000
      },
    //  轮播图是否自动播放
    autoplay: false,
    //  设置动画属性
    animationData: {},
    //  下拉的选择框是否显示
    showDown: false,
    //  监听左右滑动的始末坐标
    touchS: [0, 0],
    touchE: [0, 0],
    //  获取的热门商品的id
    goodsId: [],
    //  获取的所有推荐商品信息参数
    goodsInfo: [],
    //  获取的下一栏的热门商品
    goodsInfo1: [],
    // 下两栏的商品
    goodsInfo2: [],
    // 下三栏的商品
    goodsInfo3: [],
    //  导航栏内的数据是否显示
    // isCate: false,
    //  导航栏内的商品展示
    targetGoods: []
  },
  //  导航栏实现点击显示
  toCate(e) {
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: '/pages/main/cate/cate?id=' + e.currentTarget.dataset.id
    })
  },
  //  点击每个商品展示详情页
  showDetail(e) {
    wx.navigateTo({
      url: '/pages/main/detail/detail?id=' + e.currentTarget.dataset.id
    })
    // console.log(e.currentTarget.dataset.id);
  },

  toCart(e) {
    getApp().globalData.cart_id = e.target.dataset.id
    console.log(e.target.dataset.id);
    wx.switchTab({
      url: '/pages/cart/cart',
    })

  },
  selectAttr: function (e) {
    console.log('选择属性');
    this.setData({
      showDown: true
    })
  },
  closeSown: function (e) {
    this.setData({
      showDown: false
    })
  },
  /**
   * Lifecycle function--Called when page load
   */
  async onLoad(options) {
    //调用函数获取云端的值;
    //获取的全局变量的url地址
    const {
      data: res
    } = await dov.get('goods', {
      data: this.data.query
    })
    if (res.meta.status !== 200) {
      wx.showToast({
        title: '获取商品失败',
      })
    }
    //  获取数据成功后的方法
    wx.showToast({
      title: '成功!',
    })
    //  处理一下数据
    // let rawGood = res.data.goods
    // this.setData({
    //   rowGood: rawGood
    // })
    let goodsId = res.data.goods.filter(n => n.is_promote === true).map(n => n.goods_id)
    // console.log(goodsId.length);
    var goods = []
    for (let i = 0; i < goodsId.length; i++) {
      var {
        data: res1
      } = await dov.get(`goods/${goodsId[i]}`)
      if (res1.meta.status !== 200) {
        return
      }
      goods.push(res1.data)
      this.setData({
        goodsInfo: goods,
        goodsInfo1: goods.slice(0, 10),
        goodsInfo2: goods.slice(11, 20),
        goodsInfo3: goods.slice(21, 30),
      })
    }
    // console.log(goods);
  },
  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function () {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function () {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function () {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function () {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function () {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function () {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function () {

  }
})